package com.aijk.web.admin.controller.sys;

import com.aijk.base.entity.SysFlag;
import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.utils.Utils;
import com.aijk.commons.web.constant.DubboVersionSystemConstant;

import com.aijk.ehm.sys.api.ModuleApi;
import com.aijk.ehm.sys.model.SysModule;
import com.aijk.ehm.sys.model.qo.ModuleQo;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.entity.Constant;
import com.aijk.web.admin.entity.JsTree;
import com.aijk.web.admin.utils.DataTableUtil;
import com.aijk.web.admin.utils.ModuleTreeUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/admin/sys/module")
public class ModuleController extends BaseController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Reference(version = DubboVersionSystemConstant.DUBBO_VERSION_SYS_MODULE_API)
    private ModuleApi moduleApi;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @RequiresPermissions("admin:sys:module:list")
    public String list_view() {
        return "sys/module_list";
    }


    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add_view(@RequestParam(name = "pid", required = false) Integer pid, Model model) {
        model.addAttribute("method", "add");
        if (Utils.isNotEmpty(pid))
            model.addAttribute("pid", pid);
        else
            model.addAttribute("pid", "0");

        /*model.addAttribute("sysflags", SysFlag.values());*/
        return "sys/module_edit";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg add(@Valid SysModule module) {
        try {
            module.setSysFlag(Constant.SYS_PE_FLAG);
            module.setCreatedate(new Date());
            moduleApi.save(module);
        } catch (ErrorCodeException e) {
            e.printStackTrace();
            return RetMsg.failed();
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update_view(@RequestParam(value = "id") Integer id, Model model) {

        SysModule module = moduleApi.get(id);
        model.addAttribute("pid", module.getPid());
        model.addAttribute("module", module);
        model.addAttribute("method", "update");
        model.addAttribute("sysflags", SysFlag.values());

        return "sys/module_edit";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg update(@Valid SysModule module) {
        try {
            module.setModifydate(new Date());
            moduleApi.updateByPrimary(module);
        } catch (ErrorCodeException e) {
            e.printStackTrace();
            return RetMsg.failed();
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @RequiresPermissions("admin:sys:module:list")
    public
    @ResponseBody
    RetMsg list(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Integer draw) {

        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        ModuleQo qo = DataTableUtil.fillQo(skey, ModuleQo.class);
        qo.setSysFlag(getShiroUser().getSysFlag());
        draw += 1;
        PageInfo<SysModule> logList = moduleApi.getPage(qo);

        iTotalRecords = logList.getTotal();
        RetMsg regMsg = new RetMsg();

        regMsg.put("data", logList.getList());
        regMsg.put("draw", draw);
        regMsg.put("recordsTotal", iTotalRecords);
        regMsg.put("recordsFiltered", iTotalRecords);

        return regMsg;
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg del(@RequestParam(value = "id") Integer id) {
        try {
            moduleApi.delete(id);
        } catch (ErrorCodeException e) {
            e.printStackTrace();
            return RetMsg.failed();
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public
    @ResponseBody
    String tree() {
        ModuleQo moduleQo = new ModuleQo();
        moduleQo.setSysFlag(getShiroUser().getSysFlag());
        moduleQo.setSortway(" orderno asc");
        List<SysModule> moduleList = moduleApi.get(moduleQo);
        List<JsTree> treedata = ModuleTreeUtils.getModuleTree(moduleList);
        return JSON.toJSONString(treedata);
    }

}
